﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework;
using SynapseGaming.LightingSystem.Rendering;
//using SynapseGaming.LightingSystem.Rendering;

namespace The_LarX
{
    public class XboxAvatar : I3DObject
    {
        public Matrix World
        {
            get;
            set;
        }

        public Vector3 Scale
        {
            get;
            set;
        }

        public Vector3 RadiansRotation
        {
            get;
            set;
        }
        public bool CalculateRotationMatrix = true;

        private Matrix rotationMatrix;
        public Matrix RotationMatrix
        {
            get { return rotationMatrix; }
            set
            {
                rotationMatrix = value;
                CalculateRotationMatrix = false;
            }
        }

        public Matrix TranslationMatrix { get; set; }

        public string Name { get; set; }

        public Vector3 Position
        {
            get;
            set;
        }

#if XBOX
        public AvatarAnimation Animation;
        public AvatarDescription Description;
        public AvatarRenderer Renderer;

        private bool Loaded;

        public Camera3D Camera;
        
        private Renderer_SunBurn SunBurnRenderer;
        private Avatar SunBurnAvatar;


#endif
        private void SetInitialValues()
        {
#if XBOX
            Scale = Vector3.One;
#endif
        }
        public XboxAvatar(Camera3D camera)
        {
#if XBOX
            SetInitialValues();
            this.Camera = camera;
            Animation = new AvatarAnimation(AvatarAnimationPreset.Wave);
            Description = AvatarDescription.CreateRandom();
            Renderer = new AvatarRenderer(Description, false);
            Loaded = true;
#endif
        }
        public XboxAvatar(Camera3D camera, Renderer_SunBurn renderer)
        {
#if XBOX
            this.SunBurnRenderer = renderer;
            SetInitialValues();
            this.Camera = camera;
            Animation = new AvatarAnimation(AvatarAnimationPreset.Wave);
            Description = AvatarDescription.CreateRandom();
            Renderer = new AvatarRenderer(Description, false);

            SunBurnAvatar = new Avatar(Renderer, Description);
            SunBurnAvatar.Visibility = SynapseGaming.LightingSystem.Core.ObjectVisibility.RenderedAndCastShadows;

            Loaded = true;
#endif
        }
        public XboxAvatar(Camera3D camera, Gamer gamer)
        {
#if XBOX
            SetInitialValues();
            this.Camera = camera;
            Animation = new AvatarAnimation(AvatarAnimationPreset.Wave);
            AvatarDescription.BeginGetFromGamer(gamer, new AsyncCallback(Finish), null);                        
#endif
        }
        void Finish(IAsyncResult Result)
        {
#if XBOX
            Description = AvatarDescription.EndGetFromGamer(Result);
            Renderer = new AvatarRenderer(Description, false);
            Loaded = true;
#endif
        }

        public virtual void Update()
        {            
#if XBOX
            Animation.Update(Engine.GameTime.ElapsedGameTime, true);

            if (CalculateRotationMatrix)
                RotationMatrix = Matrix.CreateFromYawPitchRoll(RadiansRotation.Y, RadiansRotation.X, RadiansRotation.Z);
            World = Matrix.CreateScale(Scale) * RotationMatrix * Matrix.CreateTranslation(Position);

            if (SunBurnAvatar != null)
            {
                SunBurnAvatar.SkinBones = Animation.BoneTransforms;
                SunBurnAvatar.World = World;
            }
#endif
        }
        public virtual void Draw()
        {
#if XBOX
            if (SunBurnRenderer != null)
            {
                SunBurnRenderer.SceneInterface.AvatarManager.Submit(SunBurnAvatar);
            }
            else
            {
                if (Loaded)
                {
                    Renderer.World = World;
                    Renderer.Projection = Camera.Projection;
                    Renderer.View = Camera.View;
                    Renderer.Draw(Animation);
                }
            }
#endif
        }

        
    }
}
